package com.zahb.qadx.ui.activity.topic;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.githang.statusbar.StatusBarCompat;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.model.DailyExaminationQuestionsBean;
import com.zahb.qadx.model.TestInformationBean;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.Tips;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

//培训考试

public class TheTestActivity extends BaseActivity {
    @BindView(R.id.item_name)
    TextView itemName;
    @BindView(R.id.total_score)
    TextView totalScore;
    @BindView(R.id.passing_grade)
    TextView passingGrade;
    @BindView(R.id.start_time)
    TextView startTime;
    @BindView(R.id.end_of_time)
    TextView endOfTime;
    @BindView(R.id.total_time)
    TextView totalTime;
    @BindView(R.id.the_exam_type_recyclerView)
    RecyclerView theExamTypeRecyclerView;
    private String mSourceId, relationshipId;
    private TestInformationBean mTestInformationBean;
    private BaseQuickAdapter<TestInformationBean.ExamTypeVOListBean, BaseViewHolder> mAdapter;
    private int address = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUnbinder = ButterKnife.bind(this);
        navigationInitialization();//状态初始化
        //网络请求
        theExamTypeRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    }
    private void getTheData() {
        addDisposable(RetrofitService.getNetService()
                .getTestInformation(mSourceId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        mTestInformationBean = response.getData();
                        itemName.setText(mTestInformationBean.getExamName());//考试试题名字
                        totalScore.setText("总分" + mTestInformationBean.getTotalScore());
                        passingGrade.setText("及格分" + mTestInformationBean.getPassScore());
                        startTime.setText(mTestInformationBean.getStartTime());//开始时间
                        endOfTime.setText(mTestInformationBean.getEndTime());//结束时间
                        totalTime.setText(mTestInformationBean.getExamTime() + "分钟");
                        mAdapter = new BaseQuickAdapter<TestInformationBean.ExamTypeVOListBean, BaseViewHolder>(R.layout.the_exam_layout) {
                            @Override
                            protected void convert(@NotNull BaseViewHolder baseViewHolder, TestInformationBean.ExamTypeVOListBean itme) {
                                TextView type = baseViewHolder.findView(R.id.type);
                                TextView end_of_time = baseViewHolder.findView(R.id.end_of_time);
                                if (itme.getQuestionType() == 1) {
                                    type.setText("单选题");
                                } else if (itme.getQuestionType() == 2) {
                                    type.setText("多选题");
                                } else if (itme.getQuestionType() == 3) {
                                    type.setText("判断题");
                                } else if (itme.getQuestionType() == 4) {
                                    type.setText("填空题");
                                } else if (itme.getQuestionType() == 5) {
                                    type.setText("简答题");
                                }
                                end_of_time.setText("共" + itme.getQuestionCount() + "题");
                            }
                        };
                        mAdapter.addData(mTestInformationBean.getExamTypeVOList());
                        theExamTypeRecyclerView.setAdapter(mAdapter);
                    } else {
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    Tips.RequestError(getActivity());
                }));

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_the_test;
    }

    @Override
    protected void initViews() {
        mSourceId = getIntent().getStringExtra("examId");
        relationshipId = getIntent().getStringExtra("relationshipId");
        address = getIntent().getExtras().getInt("address");
        getTheData();
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.test_information;
    }

    //导航栏初始化
    private void navigationInitialization() {
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.blue));//状态栏颜色
        mTopBarContainer.setBackgroundColor(getResources().getColor(R.color.blue));//头部布局颜色
        mTitle_view.setTextColor(getResources().getColor(R.color.white));//标题颜色
        Drawable drawable = getResources().getDrawable(R.mipmap.fanhui);//返回箭头
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 设置边界
        // param 左上右下
        mTopBarStartText.setCompoundDrawables(drawable, null, null, null);
        mTopBarStartText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void monClick(View view) {
        if (address == 1) {
            if (mTestInformationBean == null) {
                return;
            }
            Date date = new Date();
            String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            if (nowTime.compareTo(mTestInformationBean.getStartTime()) < 0) {
                //ToastUtils.s(MyExamActivity.this,"考试暂未开始，请按规定时间参加考试");
                showBindToast("考试暂未开始，请按规定时间参加考试");
                return;
            }
            if (nowTime.compareTo(mTestInformationBean.getEndTime()) > 0) {
                //ToastUtils.s(MyExamActivity.this,"考试已经结束，无法参加考试");
                showBindToast("考试已经结束，无法参加考试");
                return;
            }
            if (mTestInformationBean.getExamTotalCount() <= mTestInformationBean.getExamCount()) {
                //ToastUtils.s(MyExamActivity.this,"您的考试次数已用完，暂时无法进行考试！");
                showBindToast("您的考试次数已用完，暂时无法进行考试！");
                return;
            }
        }


        Intent intent = new Intent(TheTestActivity.this, ExamWillBeginActivity.class);
        intent.putExtra("examId", mSourceId);
        intent.putExtra("relationshipId", relationshipId);
        startActivity(intent);
        finish();
    }
}
